package com.qing.deep_in.springboot.chapter4.proxy;

import org.aopalliance.intercept.Interceptor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author guoqf
 * @date 2021/8/20 13:54
 */
public class ProxyBean implements InvocationHandler {

    private Object target;

    private Object proxy;

    public static Object getProxyInstance(Object target, Interceptor interceptor) {
        ProxyBean bean = new ProxyBean();
        bean.target = target;
        Object proxyInstance = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), bean);
        bean.proxy = proxyInstance;
        return proxyInstance;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return method.invoke(proxy,args);
    }
}
